/*
 * $Id$
 */
package py.firefly.web;

import py.firefly.Handler;
import py.firefly.Route;

import py.firefly.request.Request;
import py.firefly.request.Response;
import py.firefly.request.RequestParameters;

import py.firefly.resolution.Tiles;

import py.firefly.util.Log;

import static py.firefly.Route.*;
import py.firefly.util.JsonObject;
import py.firefly.web.dict.DictionaryService;
import py.firefly.web.dict.DictionaryServiceFactory;

/**
 *
 * @author myang
 */
public class Routes {

    private static DictionaryService DS = DictionaryServiceFactory.memory();

    private static Log log = Log.of("py.firefly.web");

    /**
     */ 
    public static Route DICT = GET("/dict", new Handler() {
        @Override
        public Tiles handle(final Request req, final Response resp) {
            return new Tiles().put("body", "dict.jsp");
        }
    });

    public static Route DICT_ = GET("/dict/{word}[{idx=0}]", new Handler() {
        @Override
        public Tiles handle(final Request req, final Response resp) {
            final RequestParameters p = req.params();
            final String word = p.get("word");
            final int idx = p.getInt("idx");
            log.info("word => {}, idx => {}", word, idx);
            req.attr("word", JsonObject.toString(DS.selectWord(word, idx)));
            return new Tiles().put("body", "dict.jsp").put("ng-app", "app-dict");
        }
    });

    public static Route ARTICLES = GET("/articles", new Handler() {
        @Override
        public Tiles handle(final Request req, final Response resp) {
            return new Tiles().put("body", "articles.jsp");
        }
    });

    public static Route ROOT = GET("/", new Handler() {
        @Override
        public Tiles handle(final Request req, final Response resp) {
            return new Tiles().put("body", "index.jsp");
        }
    });
}
